package com.cn.bulu.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransOrderQueryModel;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.request.AlipayFundTransOrderQueryRequest;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransOrderQueryResponse;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.cn.bulu.constant.PayMethod;
import com.cn.bulu.constant.TypeOfIndent;
import com.cn.bulu.constant.WorkerType;
import com.cn.bulu.dao.*;
import com.cn.bulu.pojo.*;
import com.cn.bulu.service.PriceService;
import com.cn.bulu.service.WorkerService;
import com.cn.bulu.service.WorkerWalletRecordService;
import com.cn.bulu.utils.*;
import com.cn.bulu.utils.ClothCurtain.Arith;
import com.cn.bulu.utils.MD5Utils.MD5Utils;
import com.cn.bulu.utils.alipay.config.AlipayConfig;
import com.cn.bulu.utils.alipay.util.AlipayParams;
import com.cn.bulu.utils.jiguang.Jiguang;
import com.cn.bulu.utils.jiguang.JpushSendUtil;
import com.cn.bulu.utils.redis.JedisUtil;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;

@Service("workerService")
public class WorkerServiceImpl implements WorkerService {

	private static Logger logger = Logger.getLogger(WorkerServiceImpl.class);

	@Resource
	private WorkerDao workerDao;
	@Resource
	private PriceDao priceDao;
	@Resource
	private FactoryDao factoryDao;
	@Resource
	private RechargeDao rechargeDao;
	@Resource
	private MoneyRecordDao moneyRecordDao;
	@Resource
	private BankCardDao bankCardDao;
	@Resource
	private IndentDao indentDao;
	@Resource
	private WorkerWalletRecordService workerWalletRecordService;
	@Resource
	private PriceService priceService;

	// 注册
	public BuluResult register(/* String phone, String password */String accont) {
		BuluResult buluResult = new BuluResult();
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject object = getAccountMap(accont, 1);
		String phone = object.getString("phone");
		String password = object.getString("password");
		String weiyi = MD5Utils.getUUID();
		password = MD5Utils.MD5(password + weiyi);
		BuluResult isPhone = isPhone(phone);
		if (isPhone.getStatus() == 1) {
			buluResult.setStatus(1);
			buluResult.setMessage("手机号已经注册过了!");
			return buluResult;
		}
		map.put("phone", phone);
		map.put("password", password);
		map.put("weiyi", weiyi);
		Date join_date = new Date();
		map.put("join_date", join_date);// 用户注册的时间
		workerDao.insertWorker(map);
		// 注册用户的时候 把意向用户 的状态改为已注册
		workerDao.updatePotentialWorkerStatusByPhone(phone);
		int workerId = workerDao.getWorkerId(map);
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("workerId", workerId);
		buluResult.setStatus(0);
		buluResult.setData(map2);
		return buluResult;
	}

	/**
	 * 对账号和密码进行解密
	 * 
	 * @param account
	 * @param type
	 *            1:代表解密后为JSONObject对象 2:代表解密的是密码
	 * @return
	 */
	private JSONObject getAccountMap(String account, int type) {
		JSONObject map = new JSONObject();
		try {
			account = AESCipher.aesDecryptString(account, Constant.ENCRYPTKEY);
			map = JSONObject.parseObject(account);
			logger.error("<=========工人登陆的账户和密码：" + map);
		} catch (Exception e) {
			logger.error("===============工人解密时出现错误" + account + "==================");
			return map;
		}
		return map;
	}

	// 判断手机号是否可以用
	public BuluResult isPhone(String phone) {
		int result = workerDao.getWorkerNumber(phone);
		BuluResult buluResult = new BuluResult();
		if (result == 0) {
			buluResult.setStatus(0);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("手机号已经注册过了!");
		}
		return buluResult;
	}

	// 完善工人信息
	public BuluResult completeWorkerData(Integer workerId, String name, String province, String city,
			String workerSpaces, String headImg, Integer workerAge) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("workerId", workerId);
		map.put("name", name);
		map.put("workerAge", workerAge);
		map.put("workerSpace", province + city);

		String provinceCode = workerDao.selectProvinceCodeForWorker(province);
		String cityCode = workerDao.selectCityCodeForWorker(city);
		String workerSpaceCodeStr = provinceCode + "/" + cityCode;

		map.put("workerSpace", province + city);
		map.put("workerSpaceCodeStr", workerSpaceCodeStr);
		map.put("provinceCode", provinceCode);
		map.put("cityCode", cityCode);

		map.put("headImg", headImg);
		workerDao.completeWorkerData(map);

		// 删除旧的工作区域
		workerDao.deleteOldWorkerSpace(workerId);

		JSONArray jsonArray = JSONObject.parseArray(workerSpaces);
		for (int a = 0; a < jsonArray.size(); a++) {
			// 把工作区域存到表里面
			JSONObject object = jsonArray.getJSONObject(a);
			Map<String, Object> send = new HashMap<>();
			send.put("workerId", workerId);
			send.put("workerSpace", object.get("workerSpace"));
			send.put("workerSpaceCode", object.get("workerSpaceCode"));
			workerDao.insertWorkerSpace(send);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData("完善信息成功!");
		return buluResult;
	}

	// 修改密码
	public BuluResult updateWorkerPassword(/* String phone, String password */String account) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject object = getAccountMap(account, 1);
		String phone = object.getString("phone");
		String password = object.getString("password");
		String weiyi = MD5Utils.getUUID();
		password = MD5Utils.MD5(password + weiyi);
		map.put("phone", phone);
		map.put("weiyi", weiyi);
		map.put("password", password);
		workerDao.updatePassword(map);
		BuluResult buluResult = new BuluResult();// 所有返回的都是BuluResult,创建接口对象存入相应的数据
		buluResult.setStatus(0);
		return buluResult;
	}

	// 登录
	public BuluResult login(/* String phone, String password */String account, String imei,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 根据手机号查询出工人的uuid
		JSONObject object = getAccountMap(account, 1);
		String phone = object.getString("phone");
		String password = object.getString("password");
		String weiyi = workerDao.selectWeiyi(phone);
		password = MD5Utils.MD5(password + weiyi);
		map.put("phone", phone);
		map.put("password", password);
		int result = workerDao.getWorkerNumber2(map);

		BuluResult buluResult = new BuluResult();
		if (result == 1) {
			Worker worker = workerDao.getWorkerId2(map);
			// 得到工人的id
			String name = workerDao.getspace(map);
			if (name != null) {
				if (worker.getIsDisable() != 1) {
					// 老的imei
					String imei1 = workerDao.selectImeiByWorkerId(worker.getWorkerId());
					logger.error("imei1==" + imei1 + "==imei==" + imei);
					// 如果当前app的第一次登陆
					// 类似于SESSIONID
					String md5SessionID = MD5Utils.MD5(String.valueOf(new Date().getTime()));
					worker.setSign(md5SessionID);
					JedisUtil.keepSessionIDForApp(md5SessionID, worker.getWorkerId().toString(), 7);
					Map<String, Object> m = new HashMap<>();
					m.put("workerId", worker.getWorkerId());
					m.put("imei", imei);
					workerDao.updateImeiByWorkerId(m);
					if (imei1 != null && !imei.equals(imei1)) {// 此次登陆的设备码和上次不一样，发送下线通知
						// 已经有人登陆了 需要注销掉以前用户 然后让新用户登陆
						// 极光推送消息注销老登陆用户
						Map<String, String> typeMap = new HashMap<String, String>();
						typeMap.put("msgType", String.valueOf(2));
						JpushSendUtil.sendPushMessage(imei1, "提示！你的账号存在风险，一下异地下号", typeMap);
					}
					// 查询工作区域
					List<Map<String, String>> list = workerDao.selectWorkerSpace(worker.getWorkerId());
					worker.setWorkerSpaces(list);
					buluResult.setData(worker);
					buluResult.setStatus(0);
				} else {
					buluResult.setStatus(0);
					buluResult.setData(worker);
					buluResult.setMessage("该账号已被停用!!!");
				}
			} else {
				Map<String, Object> map2 = new HashMap<>();
				map2.put("workerId", worker.getWorkerId());
				buluResult.setData(map2);
				buluResult.setStatus(2);
				buluResult.setMessage("信息未完善!!!");
			}
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("手机号或者密码不对!");
		}
		return buluResult;

	}

	// 修改头像
	public BuluResult updateHeadImg(HttpServletRequest request, String headImg) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("workerId", workerId);
		map.put("headImg", headImg);
		workerDao.updateHeadImg(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询可接订单
	public BuluResult selectKejie(HttpServletRequest request, Integer type) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Worker worker = workerDao.selectWorker(workerId);
		String city = worker.getWorkerSpace();
		// 根据id查询工作区域
		List<String> areaCode = workerDao.selectWorkerSpaces(workerId);
		logger.error("areaCode=====" + areaCode);
		BuluResult buluResult = new BuluResult();
		// 没有选择工作区域或者工人被限制权限为不能查看可以接订单状态
		if (areaCode.size() == 0 || worker.getCanGetIndent() == 1) {
			buluResult.setStatus(1);
			buluResult.setMessage("亲，抱歉，暂时没有数据哦!!!");
		} else {
			Map<String, Object> map = new HashMap<>();
			map.put("type", type);
			map.put("cityArea", areaCode);
			List<Map<String, Object>> list = workerDao.selectCIndent(map);
			if (list != null && list.size() > 0) {
				for (int a = 0; a < list.size(); a++) {
					if ((Integer) list.get(a).get("type") == 1) {
						list.get(a).put("roomNumB", list.get(a).get("roomNumB"));
					}
					if (list.get(a).get("baseAddress") == null) {
						list.get(a).put("baseAddress", "");
						list.get(a).put("detailAddress", "");
					}
				}
				buluResult.setStatus(0);
				buluResult.setData(list);
			} else {
				buluResult.setStatus(1);
				buluResult.setMessage("亲，抱歉，暂时没有数据哦!!!");
			}
		}
		return buluResult;
	}

	// 点击我要接单
	public BuluResult binging(HttpServletRequest request, Integer indentId) {
		// 参数准备
		BuluResult buluResult = new BuluResult();
		// 获取商家id
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 查看订单是否没人接
		Integer WR = workerDao.selectNoBinging(indentId);
		if (WR != null) {
			buluResult.setStatus(1);
			buluResult.setMessage("手慢一步，该订单已被其他工人抢走!!!");
		} else {
			Date acceptTime = new Date();
			Map<String, Object> map = new HashMap<>();
			map.put("acceptTime", acceptTime);
			map.put("workerId", workerId);
			map.put("indentId", indentId);
			int result = workerDao.updateReceive(map);
			if (result > 0) {
				buluResult.setStatus(0);
			} else {
				buluResult.setStatus(1);
				buluResult.setMessage("手慢一步，该订单已被其他工人抢走!!!");
			}
		}
		return buluResult;
	}

	// 查询已接订单
	public BuluResult YesBinding(HttpServletRequest request, Integer type) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> map = new HashMap<>();
		map.put("workerId", workerId);
		map.put("type", type);
		List<Map<String, Object>> list = workerDao.selectYIndent(map);
		logger.error("list=====2" + list);
		BuluResult buluResult = new BuluResult();
		if (list != null && list.size() > 0) {
			for (int a = 0; a < list.size(); a++) {
				if ((Integer) list.get(a).get("type") == 1) {
					list.get(a).put("roomNumB", list.get(a).get("roomNumB"));
				}
				if (list.get(a).get("baseAddress") == null) {
					list.get(a).put("baseAddress", "");
					list.get(a).put("detailAddress", "");
				}
			}
			buluResult.setStatus(0);
			buluResult.setData(list);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("亲，还没有已接的订单呢!!!");
		}
		return buluResult;
	}

	// 查询钱包
	public BuluResult selectWallet(HttpServletRequest request) {
		BuluResult buluResult = new BuluResult();
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Worker worker = workerDao.selectWorker(workerId);
		if (null == worker.getWithdrawalStatus()) {
			worker.setWithdrawalStatus(2);// 默认值为2，没有提现能力
		}
		// 查询默认支付宝信息
		Map<String, Object> map = workerDao.selectDefultBank(workerId);
		if (map != null) {
			worker.setAlipayNum((String) map.get("alipayNum"));
			worker.setBoundBank((String) map.get("boundBank"));
			/* worker.setBankNumber((String) map.get("bankNumber")); */
			worker.setBankCardId((Integer) map.get("bankCardId"));
		} else {
			worker.setBoundBank("无");
			worker.setBankNumber("无");
			worker.setAlipayNum("你目前还没有支付宝账号");
		}
		buluResult.setStatus(0);
		buluResult.setData(worker);
		return buluResult;
	}

	// 查询应扣款
	public BuluResult selectPenalties(HttpServletRequest request, Integer indentId) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		String city = workerDao.selectKoukuan(workerId);
		int territoryId = workerDao.selectPenalties(city);
		Map<String, Double> punishMap = workerDao.selectBaifenbi(territoryId);
		BuluResult buluResult = new BuluResult();
		Map<String, Object> indentMap = workerDao.selectIndent(indentId);
		int status = (int) indentMap.get("status");
		if (status == 0 || status == 1) {
			buluResult.setData(punishMap.get("punishWorker"));
		} else if (status == 5) {
			buluResult.setData(punishMap.get("punishClient"));
		} else if (status == 7) {
			buluResult.setMessage("商家已经取消订单");
			buluResult.setData(0);
			buluResult.setStatus(1);
			return buluResult;
		}
		buluResult.setStatus(0);
		return buluResult;
	}

	// 放弃订单扣除金额
	public BuluResult updateWallet(HttpServletRequest request, Integer indentId) {
		// 准备返回数据
		BuluResult buluResult = new BuluResult();
		// 获取工人id
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 找到该订单的状态
		Map<String, Object> indentMap = workerDao.selectIndent(indentId);
		int status = (int) indentMap.get("status");
		if (status == 1 || status == 5) {
			int indentResource=Integer.parseInt(indentMap.get("indentSource").toString());//订单来源 '0鹿客帮app下单，1鹿客帮网页下单，2家居下单',
			int update=0;
			Map<String, Object>setMap=new HashMap<>();
			if (indentResource==2) {
				// 跟新订单状态
				setMap.put("indentId", indentId);
				setMap.put("status", 9);
				update = workerDao.updateIndent(setMap);
			}else {
				// 跟新订单状态
				setMap.put("indentId", indentId);
				setMap.put("status",0);
				update = workerDao.updateIndent(setMap);
			}			
			if (update > 0) {
				// 订单状态修改成功以后将本次工人对订单操作的数据同样修改为无效状态
				updateGoodsOfIndent(indentMap, indentId);
				// 找到该工人城市扣款价位
				String city = workerDao.selectKoukuan(workerId);
				int territoryId = workerDao.selectPenalties(city);
				Map<String, Double> punishMap = workerDao.selectBaifenbi(territoryId);
				// 准备罚款
				Double wallet = 0.00;
				wallet = status == 1 ? (Double) punishMap.get("punishWorker") : (Double) punishMap.get("punishClient");
				// 更新工人钱包
				Map<String, Object> map = new HashMap<>();
				map.put("workerId", workerId);
				map.put("koukuan", wallet);
				int result = workerDao.updateWallet(map);
				// 保存工人钱包变动记录
				/*
				 * workerId, indentId, 2, mainMoney, addMoney, subMoney,
				 * finalGetMoney, finalLostMoney, walletRecharge,
				 * walletWithdrawals, type, status, remark, isDelete, method }
				 */
				if (result > 0) {
					workerWalletRecordService.insertWorkerWalletRecord(workerId, indentId, 2, null, null, wallet, null,
							wallet, null, null, WorkerType.WORKER_CANCLE.getIndex(), 1, "工人取消已接订单", 0,
							PayMethod.BALABCEPAY.getMethod());
				}
				//清空原有数据
				int type = (int) indentMap.get("type");
				if(type==0){//测量
					//清除测量图片
					indentDao.clearIndentPictures(indentId);
					//清除测量数据
					indentDao.clearCLIndentDatas(indentId);
				}else if(type==1){//安装
					//清除安装图片
					indentDao.clearIndentPictures(indentId);
				}
				// 重新进行推送 0推送订单 1推送消息
				Jiguang.JpushIndent(indentMap);
				buluResult.setStatus(0);
			}
		} else if (status == 7) {
			buluResult.setStatus(1);
			buluResult.setMessage("订单可能已被商家取消");
		}
		// 查询信誉保障金
		Double creDit = workerDao.selectCreDit(workerId);
		buluResult.setData(creDit);
		return buluResult;
	}

	/**
	 * 工人取消订单的时候需要将工人对订单的一些操作标记为失效状态 xiaGuanghui 2017年5月31日 下午6:28:29
	 * 
	 * @param indentMap
	 * @param indentId
	 */
	private void updateGoodsOfIndent(Map<String, Object> indentMap, Integer indentId) {
		int type = (Integer) indentMap.get("type");
		// type=0,订单为测量订单
		if (type == 0) {
			// 查找测量订单对应的goods属性
			Map<String,Object> param = new HashMap<>();
			param.put("indentId",indentId);
			param.put("isDeleted","0");
			List<Map<String, Object>> list = workerDao.selectGoods(param);
			int updateNum = (null == list || list.size() == 0) ? 0
					: workerDao.updateGoodsIndentByGoodsIndentId(indentId);
			logger.error("取消工人对测量订单" + updateNum + "次的操作");
			// type=1,订单为安装订单
		} else if (type == 1) {
			List<Picture> pictureList = workerDao.selectinstallByIndentId(indentId);
			if (null == pictureList || pictureList.size() == 0) {
				return;
			}
			for (int index = 0; index < pictureList.size(); index++) {
				Picture picture = pictureList.get(index);
				workerDao.updatePictureByPictureId(picture.getPictureId());
			}
			// 其他的不进行任何操作
		} else {
			return;
		}
	}

	// 工人点击出发
	public BuluResult insertJingwei(Integer indentId, String jingdu, String weidu) {
		BuluResult buluResult = new BuluResult();
		// 修改订单状态为工人已出发
		Map<String,Object> map1 = workerDao.selectIndent(indentId);
		int result = 0;
		if (1 == ((Integer)map1.get("status"))){
			result = workerDao.updateIndentCF(indentId);
		}
		if (result > 0) {
			Map<String, Object> map = new HashMap<>();
			map.put("jingdu", jingdu);
			map.put("weidu", weidu);
			map.put("indentId", indentId);
			workerDao.insertJingwei(map);
			buluResult.setStatus(0);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("该订单可能已被商家取消");
		}
		return buluResult;
	}

	// 查询可接订单各个类型数量
	/**
	 * 根据每一个员工的ID的查询可接受的订单
	 */
	public BuluResult selectKejieNumber(HttpServletRequest request) {
		// 查询到用户的的工作ID
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Worker worker = workerDao.selectWorker(workerId);
		// 查询到cityArea 比如说河南省郑州市金水区 河南省郑州市中原区
		// 根据id查询工作区域code
		List<String> areaCode = workerDao.selectWorkerSpaces(workerId);
		Integer celiang = 0;
		Integer anzhuang = 0;
		Integer weixiu = 0;
		if (areaCode.size() != 0) {
			// 可接测量订单数量
			celiang = workerDao.selectCeliang(0, areaCode);
			// 可接安装订单数量
			anzhuang = workerDao.selectCeliang(1, areaCode);
			// 可接维修订单数量
			weixiu = workerDao.selectCeliang(2, areaCode);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("celiang", celiang);
		map.put("anzhuang", anzhuang);
		map.put("weixiu", weixiu);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	// 查询已接订单各个类型的数量
	public BuluResult selectYijie(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> send = new HashMap<>();
		send.put("workerId", workerId);
		// 已接测量订单数量
		send.put("type", 0);
		Integer celiang = workerDao.selectYijie(send);
		// 已接安装订单数量
		send.put("type", 1);
		Integer anzhuang = workerDao.selectYijie(send);
		// 已接维修订单数量
		send.put("type", 2);
		Integer weixiu = workerDao.selectYijie(send);
		Map<String, Object> map = new HashMap<>();
		map.put("celiang", celiang);
		map.put("anzhuang", anzhuang);
		map.put("weixiu", weixiu);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	// 查询已完成订单
	public BuluResult selectYiwcheng(HttpServletRequest request, Integer type, Integer page) {
		page = 10 * (page - 1);
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> map = new HashMap<>();
		map.put("workerId", workerId);
		map.put("type", type);
		map.put("page", page);
		List<Map<String, Object>> list = workerDao.selectYiwcheng(map);
		logger.error("list=====3" + list);
		BuluResult buluResult = new BuluResult();
		if (list != null && list.size() > 0) {
			buluResult.setStatus(0);
			buluResult.setData(list);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("没有已完成订单!!!");
		}
		return buluResult;
	}

	// 根据订单号查询详情
	public BuluResult selectXiangqing(Integer indentId) {
		// 根据订单号查询有多少个商品
		Map<String,Object> param = new HashMap<>();
		param.put("indentId",indentId);
		param.put("isDeleted","0");

		List<Map<String, Object>> list = workerDao.selectGoods(param);
		for (int a = 0; a < list.size(); a++) {
			if (list.get(a).get("sampleId") != null) {
				String sampleImg = workerDao.selectSampleImg((Integer) list.get(a).get("sampleId"));
				list.get(a).put("sampleImg", sampleImg);
			}
			if (list.get(a).get("locationId") != null) {
				String locationContent = workerDao.selectLocationConent((Integer) list.get(a).get("locationId"));
				list.get(a).put("locationContent", locationContent);
			} else {
				list.get(a).put("locationContent", list.get(a).get("otherRoom"));
			}
			if ((Integer) list.get(a).get("istianxie") != 0) {
				if ((Integer) list.get(a).get("type") == 0) {// 布艺帘
					// 根据商品id查询安装图片是否提交
					Integer install = workerDao.selectinstall((Integer) list.get(a).get("goodsId"));
					if (install != 0) {// 有图
						list.get(a).put("isinstallImg", 1);
					} else {// 没图
						list.get(a).put("isinstallImg", 0);
					}
					if (list.get(a).get("structure") != null) {
						// 判断是什么结构
						if ((Integer) list.get(a).get("structure") == 0) {// 单布
							// 查询布有几个尺寸
							Integer buchicun = factoryDao.selectGoodsBu((Integer) list.get(a).get("goodsId"));
							if (buchicun == 1) {
								// 根据商品号查询出尺寸
								Map<String, Object> map = factoryDao
										.selectGoodsBuchicun((Integer) list.get(a).get("goodsId"));
								list.get(a).put("buwide", map.get("wide"));
								list.get(a).put("buhigh", map.get("high"));
								// 判断是否有帘头
								if ((Integer) list.get(a).get("shadeHead") == 1) {
									list.get(a).put("touwide",
											factoryDao.selectGoodsTouchicun((Integer) list.get(a).get("goodsId")));
								}
								list.get(a).put("special", 0);
							} else {
								list.get(a).put("special", 1);
							}
						} else if ((Integer) list.get(a).get("structure") == 1) {// 单纱
							// 查询纱有几个尺寸
							Integer shachicun = factoryDao.selectGoodsSha((Integer) list.get(a).get("goodsId"));
							if (shachicun == 1) {
								// 根据商品号查询出尺寸
								Map<String, Object> map = factoryDao
										.selectGoodsShachicun((Integer) list.get(a).get("goodsId"));
								list.get(a).put("shawide", map.get("wide"));
								list.get(a).put("shahigh", map.get("high"));
								list.get(a).put("special", 0);
							} else {
								list.get(a).put("special", 1);
							}
						} else {// 布加纱
								// 查询布有几个尺寸
							Integer buchicun = factoryDao.selectGoodsBu((Integer) list.get(a).get("goodsId"));
							if (buchicun == 1) {
								// 查询纱有几个尺寸
								Integer shachicun = factoryDao.selectGoodsSha((Integer) list.get(a).get("goodsId"));
								if (shachicun == 1) {
									// 根据商品号查询出布尺寸
									Map<String, Object> map = factoryDao
											.selectGoodsBuchicun((Integer) list.get(a).get("goodsId"));
									list.get(a).put("buwide", map.get("wide"));
									list.get(a).put("buhigh", map.get("high"));
									// 根据商品号查询出纱尺寸
									Map<String, Object> map1 = factoryDao
											.selectGoodsShachicun((Integer) list.get(a).get("goodsId"));
									list.get(a).put("shawide", map1.get("wide"));
									list.get(a).put("shahigh", map1.get("high"));
									// 判断是否有帘头
									if ((Integer) list.get(a).get("shadeHead") == 1) {
										list.get(a).put("touwide",
												factoryDao.selectGoodsTouchicun((Integer) list.get(a).get("goodsId")));
									}
									list.get(a).put("special", 0);
								} else {
									list.get(a).put("special", 1);
								}
							} else {
								list.get(a).put("special", 1);
							}
						}
					}

				} else if ((Integer) list.get(a).get("type") == 1) {// 成品帘
					int flag = (int) list.get(a).get("istianxie");
					if (flag == 1) {
						Integer finishNum = factoryDao.selectGoodsFinish((Integer) list.get(a).get("goodsId"));
						if (finishNum == 1) {
							Map<String, Object> finishMap = factoryDao
									.selectFinishChicun((Integer) list.get(a).get("goodsId"));
							list.get(a).put("window", finishMap.get("window"));
							list.get(a).put("accessoriesType", finishMap.get("accessoriesType"));
							Goods goods = workerDao.selectGoodsInfo((Integer) finishMap.get("accId"));
							list.get(a).put("pinstallSite", goods.getPinstallSite());
							list.get(a).put("wide", finishMap.get("wide"));
							list.get(a).put("high", finishMap.get("high"));
							list.get(a).put("special", 0);
							List<String> imgs = workerDao.selectChengpinImgs((Integer) finishMap.get("specialId"));
							list.get(a).put("imgs", imgs);

						} else if (finishNum > 1) {
							list.get(a).put("special", 1);
						}
						// 根据商品id查询安装图片是否提交
						Integer install = workerDao.selectinstall((Integer) list.get(a).get("goodsId"));
						if (install != 0) {// 有图
							list.get(a).put("isinstallImg", 1);
						} else {// 没图
							list.get(a).put("isinstallImg", 0);
						}
					}
					if (list.get(a).get("pinstallSite") != null) {
						// 根据成品帘类型id查询出类型
						String fptBody = factoryDao.selectChnegpin((Integer) list.get(a).get("fptId"));
						list.get(a).put("fptBody", fptBody);
					}
				}
			}

		}

		// 查询有多少个房间
		// List<String> locations = workerDao.selectLocation(indentId);
		// List<String> otherRoom = workerDao.selectOtherRoom(indentId);
		// List<Map<String, Object>> list = new ArrayList<>();
		// Map<String, Object> send = new HashMap<>();
		// send.put("indentId", indentId);
		// if(locations!=null&&locations.size()>0){
		// for(int a=0;a<locations.size();a++){
		// Map<String, Object> map = new HashMap<>();
		// map.put("room", locations.get(a));
		// send.put("locationContent", locations.get(a));
		// List<Goods> goods = workerDao.selectXGoods(send);
		// for(int b=0;b<goods.size();b++){
		// List<String> peijian =
		// workerDao.selectPeijian(goods.get(b).getGoodsId());
		// goods.get(b).setPeijian(peijian);
		// if(goods.get(b).getSampleId()!=null){
		// String sampleImg =
		// workerDao.selectSampleImg(goods.get(b).getSampleId());
		// goods.get(b).setSampleImg(sampleImg);
		// }
		// }
		// map.put("goods", goods);
		// list.add(map);
		// }
		// }
		// if(otherRoom!=null&&otherRoom.size()>0){
		// for(int a=0;a<otherRoom.size();a++){
		// Map<String, Object> map = new HashMap<>();
		// map.put("room", otherRoom.get(a));
		// send.put("otherRoom", otherRoom.get(a));
		// List<Goods> goods = workerDao.selectQGoods(send);
		// for(int b=0;b<goods.size();b++){
		// if(goods.get(b).getSampleId()!=null){
		// String sampleImg =
		// workerDao.selectSampleImg(goods.get(b).getSampleId());
		// goods.get(b).setSampleImg(sampleImg);
		// }
		// }
		// map.put("goods", goods);
		// list.add(map);
		// }
		// }
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

	// 查询安装房间和布纱安装位置
	public BuluResult selectLBS() {
		List<String> BS = workerDao.selectBS();
		List<Map<String, Object>> L = workerDao.selectL();
		Map<String, Object> map = new HashMap<>();
		map.put("fangjian", L);
		map.put("weizhi", BS);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	// 查询明细
	public BuluResult selectMingxi(HttpServletRequest request, Integer page) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> map = new HashMap<>();
		// 他们传过来的page初始值为1
		page = 10 * ((page == null ? 1 : page) - 1);// 计算每一页从第几条记录开始查询
		map.put("page", page);
		map.put("workerId", workerId);
		List<Map<String, Object>> list = workerDao.selectMingxi(map);
		logger.error(JSONObject.toJSON(list));
		BuluResult buluResult = new BuluResult();
		if (list != null && list.size() > 0) {
			buluResult.setStatus(0);
			buluResult.setData(list);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("暂无记录!!!");
		}
		return buluResult;
	}

	// 添加工人信息
	public BuluResult updateXinxi(HttpServletRequest request, String idnumber, String boundBank, String bankNumber,
			String imgs) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		JSONArray jsonArray = JSONArray.parseArray(imgs);
		Map<String, Object> map = new HashMap<>();
		map.put("idnumber", idnumber);
		map.put("workerId", workerId);
		workerDao.updateWorkerBank(map);
		Map<String, Object> map2 = new HashMap<>();
		map2.put("peopleId", workerId);
		map2.put("boundBank", boundBank);
		map2.put("bankNumber", bankNumber);
		workerDao.insertBank(map2);
		for (int a = 0; a < jsonArray.size(); a++) {
			Map<String, Object> send = new HashMap<>();
			send.put("workerId", workerId);
			send.put("img", jsonArray.get(a));
			workerDao.updatePicimg(send);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 工人提交信息
	// bumishu="[{window,accessoriesType,acc,pinstallSite,panzhuangfangshi,installation,saleNumber,remark,wide,high,production,imgs[]},{}]"
	// toumishu="[{accessoriesType，saleNumber，installation，tuid，},{}]"
	public BuluResult updateTijiao(Goods goods, String bumishu, String shamishu, String toumishu, String cImgs,
			String finishShade) {
//		if (goods.getSampleId() == null || goods.getSampleId() == 0) {// 样品id为空则不进行计算
			if (goods.getType() == 0) {
				if (bumishu != null) {
					JSONArray jsonArray1 = JSONArray.parseArray(bumishu);
					for (int i = 0; i < jsonArray1.size(); i++) {
						JSONObject jsonObject = (JSONObject) jsonArray1.get(i);
						// 布配件
						Goods pei = new Goods();
						pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
						pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
						pei.setPinstallSite(jsonObject.getString("pinstallSite"));
						if (goods.getInstallCase() != null && goods.getInstallCase() != 0) {
							pei.setNumber(2);
						} else {
							pei.setNumber(1);
						}
						pei.setPanzhuangfangshi(jsonObject.getString("panzhuangfangshi"));
						pei.setAcc(jsonObject.getString("acc"));
						workerDao.insertpeijian(pei);
						if (goods.getInstallCase() != null && goods.getInstallCase() != 0) {
							Goods_special specialsha = new Goods_special();
							// specialsha.setAccId(pei.getGoodsId());
							specialsha.setInstallSite(jsonObject.getString("installSite"));
							specialsha.setAccessoriesType(jsonObject.getString("accessoriesType"));
							specialsha.setWindow(jsonObject.getString("window"));
							specialsha.setWide(jsonObject.getDouble("wide"));
							specialsha.setHigh(jsonObject.getDouble("high"));
							specialsha.setProduction(jsonObject.getInteger("production"));
							specialsha.setType(1);
							specialsha.setRemark(jsonObject.getString("remark"));
							int goodsIdChild = goods.getGoodsId() * 10 + i;
							specialsha.setGoodsIdChild(goodsIdChild);
							specialsha.setGoodsId(goods.getGoodsId());
							specialsha.setWhatStructure(2);
							workerDao.insertSpecial(specialsha);
						}
						// 存到数据库中并获得id
						Goods_special special = new Goods_special();
						special.setAccId(pei.getGoodsId());
						special.setInstallSite(jsonObject.getString("installSite"));
						special.setAccessoriesType(jsonObject.getString("accessoriesType"));
						special.setWindow(jsonObject.getString("window"));
						special.setWide(jsonObject.getDouble("wide"));
						special.setHigh(jsonObject.getDouble("high"));
						special.setProduction(jsonObject.getInteger("production"));
						special.setType(0);
						special.setRemark(jsonObject.getString("remark"));
						special.setGoodsId(goods.getGoodsId());
						int goodsIdChild = goods.getGoodsId() * 10 + i;
						special.setGoodsIdChild(goodsIdChild);
						special.setAccId(pei.getGoodsId());
						special.setWhatStructure(2);
						workerDao.insertSpecial(special);
						JSONArray imgs = (JSONArray) jsonObject.get("imgs");
						for (int s = 0; s < imgs.size(); s++) {
							// 把图片存到图片表里
							Map<String, Object> imgmap = new HashMap<>();
							imgmap.put("specialId", special.getSpecialId());
							imgmap.put("img", imgs.get(s));
							imgmap.put("paizhaoType", 0);
							workerDao.insertImg(imgmap);
						}
					}
				}
				if (shamishu != null) {
					// 不为空证明有纱
					JSONArray jsonArray1 = JSONArray.parseArray(shamishu);
					for (int a = 0; a < jsonArray1.size(); a++) {
						JSONObject jsonObject = (JSONObject) jsonArray1.get(a);
						// 存到数据库中并获得id
						Goods_special special = new Goods_special();
						// 纱配件
						if (goods.getInstallCase() == null || goods.getInstallCase() == 0) {
							Goods pei = new Goods();
							pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
							pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
							pei.setPinstallSite(jsonObject.getString("pinstallSite"));
							pei.setNumber(1);
							pei.setPanzhuangfangshi(jsonObject.getString("panzhuangfangshi"));
							pei.setAcc(jsonObject.getString("acc"));
							workerDao.insertpeijian(pei);
							special.setAccId(pei.getGoodsId());
						}
						special.setInstallSite(jsonObject.getString("installSite"));
						special.setAccessoriesType(jsonObject.getString("accessoriesType"));
						special.setWindow(jsonObject.getString("window"));
						special.setWide(jsonObject.getDouble("wide"));
						special.setHigh(jsonObject.getDouble("high"));
						special.setProduction(jsonObject.getInteger("production"));
						special.setType(1);
						special.setRemark(jsonObject.getString("remark"));
						special.setGoodsId(goods.getGoodsId());
						int goodsIdChild = goods.getGoodsId() * 10 + a;
						special.setGoodsIdChild(goodsIdChild);
						special.setWhatStructure(1);
						workerDao.insertSpecial(special);
						JSONArray imgs = (JSONArray) jsonObject.get("imgs");
						for (int s = 0; s < imgs.size(); s++) {
							// 把图片存到图片表里
							Map<String, Object> imgmap = new HashMap<>();
							imgmap.put("specialId", special.getSpecialId());
							imgmap.put("img", imgs.get(s));
							imgmap.put("paizhaoType", 0);
							workerDao.insertImg(imgmap);
						}
					}
				}
				if (toumishu != null) {
					goods.setShadeHead(1);
					// 不为空证明有帘头
					JSONArray jsonArray1 = JSONArray.parseArray(toumishu);
					for (int a = 0; a < jsonArray1.size(); a++) {
						JSONObject jsonObject = (JSONObject) jsonArray1.get(a);
						// 帘头配件
						Goods pei = new Goods();
						pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
						pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
						pei.setAcc(jsonObject.getString("acc"));
						pei.setParsOfPars(jsonObject.getString("parsOfPars"));
						workerDao.insertpeijian(pei);
						// 存到数据库中并获得id
						Goods_special special = new Goods_special();
						special.setWide(jsonObject.getDouble("wide"));
						special.setAccId(pei.getGoodsId());
						special.setInstallSite(jsonObject.getString("installSite"));
						special.setType(2);
						special.setAccessoriesType(jsonObject.getString("accessoriesType"));
						special.setRemark(jsonObject.getString("remark"));
						special.setGoodsId(goods.getGoodsId());
						int goodsIdChild = goods.getGoodsId() * 10 + a;
						special.setGoodsIdChild(goodsIdChild);
						special.setWhatStructure(3);
						workerDao.insertSpecial(special);
						if ("不满墙(无窗帘盒)".equals(jsonObject.getString("installSite"))) {
							// num为图片地址
							// 把图片存到图片表里
							Map<String, Object> imgmap = new HashMap<>();
							imgmap.put("specialId", special.getSpecialId());
							imgmap.put("img", jsonObject.getString("num"));
							imgmap.put("paizhaoType", 0);
							workerDao.insertImg(imgmap);
						}
					}
				} else {
					goods.setShadeHead(0);
				}
				goods.setIstianxie(1);

				// 家居单，工人选择韩褶或插钩款（3）时应该对应家居这边的韩褶款（1）
				if (goods.getStyle() == 3 && workerDao.selectIndentByIndentId(workerDao.selectIndentIdByGoodsId(goods.getGoodsId()).getIndentId()).getIndentSource() == 2){
					goods.setStyle(1);
				}
				workerDao.updateGoods(goods);
			} else {// 成品帘无法计算
				if (null != finishShade) {
					parseAndAddFinishShade(finishShade, goods);
					/*
					 * if (cImgs != null) { JSONArray jsonArrayCpl =
					 * JSONObject.parseArray(cImgs); for (int g = 0; g <
					 * jsonArrayCpl.size(); g++) { Map<String, Object> cpl = new
					 * HashMap<>(); cpl.put("goodsId", goods.getGoodsId());
					 * cpl.put("img", jsonArrayCpl.getString(g));
					 * workerDao.insertChengPinLianImg(cpl); } }
					 */
				}
			}
//		}
//		else {
//			if (goods.getType() == 0) {// 布艺帘
//				// 工人拍照 分为 测量 安装 维修 每一个都是多张图片
//				// 根据商品ID 查询构成的主布 配布纱 什么的
//				List<Composition> list = workerDao.selectGoucheng(goods.getGoodsId());
//				// 判断一下有一个还是多个主布
//				int x = 0;// 主布数量
//				int c = 0;
//				boolean p = false;// 是否有花边
//				int o = 0;// 花边下标
//				int linantou = 0;
//				for (int a = 0; a < list.size(); a++) {
//					if (list.get(a).getType() == 1) {
//						x++;
//						c = a;
//					}
//					if (list.get(a).getType() == 6) {// 有花边
//						p = true;
//						o = a;
//					}
//					if (list.get(a).getType() == 0) {
//						linantou = a;
//					}
//
//				}
//				Double sumPrice = 0.0;
//				// 根据商品id查询样品
//				Sample sample = workerDao.selectSample(goods.getGoodsId());
//				if (bumishu != null) {
//					JSONArray jsonArray1 = JSONArray.parseArray(bumishu);
//					for (int i = 0; i < jsonArray1.size(); i++) {
//						JSONObject jsonObject = (JSONObject) jsonArray1.get(i);
//						// 布配件
//						Goods pei = new Goods();
//						pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
//						pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
//						pei.setPinstallSite(jsonObject.getString("pinstallSite"));
//						if (goods.getInstallCase() != null && goods.getInstallCase() != 0) {
//							pei.setNumber(2);
//						} else {
//							pei.setNumber(1);
//						}
//						pei.setPanzhuangfangshi(jsonObject.getString("panzhuangfangshi"));
//						pei.setAcc(jsonObject.getString("acc"));
//						workerDao.insertpeijian(pei);
//						if (goods.getInstallCase() != null && goods.getInstallCase() != 0) {
//							Goods_special specialsha = new Goods_special();
//							specialsha.setInstallSite(jsonObject.getString("installSite"));
//							specialsha.setAccessoriesType(jsonObject.getString("accessoriesType"));
//							specialsha.setWindow(jsonObject.getString("window"));
//							specialsha.setWide(jsonObject.getDouble("wide"));
//							specialsha.setHigh(jsonObject.getDouble("high"));
//							specialsha.setProduction(jsonObject.getInteger("production"));
//							specialsha.setType(1);
//							specialsha.setGoodsId(goods.getGoodsId());
//							int goodsIdChild = goods.getGoodsId() * 10 + i;
//							specialsha.setGoodsIdChild(goodsIdChild);
//							specialsha.setWhatStructure(1);
//							workerDao.insertSpecial(specialsha);
//						}
//						// 存到数据库中并获得id
//						Goods_special special = new Goods_special();
//						special.setAccId(pei.getGoodsId());
//						special.setInstallSite(jsonObject.getString("installSite"));
//						special.setAccessoriesType(jsonObject.getString("accessoriesType"));
//						special.setWindow(jsonObject.getString("window"));
//						special.setWide(jsonObject.getDouble("wide"));
//						special.setHigh(jsonObject.getDouble("high"));
//						special.setProduction(jsonObject.getInteger("production"));
//						special.setType(0);
//						special.setGoodsId(goods.getGoodsId());
//						int goodsIdChild = goods.getGoodsId() * 10 + i;
//						special.setGoodsIdChild(goodsIdChild);
//						special.setAccId(pei.getGoodsId());
//						special.setWhatStructure(0);
//						workerDao.insertSpecial(special);
//						JSONArray imgs = (JSONArray) jsonObject.get("imgs");
//						for (int s = 0; s < imgs.size(); s++) {
//							// 把图片存到图片表里
//							Map<String, Object> imgmap = new HashMap<>();
//							imgmap.put("specialId", special.getSpecialId());
//							imgmap.put("img", imgs.get(s));
//							imgmap.put("paizhaoType", 0);
//							workerDao.insertImg(imgmap);
//						}
//						// 我感觉直接在这算就行了 不为空证明有布 在这 还得判断一下是否有遮光里布
//						// 计算布
//						Map<String, Object> zhubu = Calculate.FixedWidePin(sample.getTimes_fold(), sample.getHem(),
//								jsonObject.getDouble("wide"), jsonObject.getDouble("high"), goods.getProduction(),
//								list);
//						logger.error(zhubu);
//						// 声明一个实际宽度
//						Double zheguanglibu = 0.0;
//						// TODO
//						if (x > 1) {// 多种主布拼接
//							@SuppressWarnings("unchecked")
//							List<Map<String, Object>> zhubulist = (List<Map<String, Object>>) zhubu.get("zhubuList");
//							// 查询主布是否有横拼的？？
//							// 判断是否是特价
//							if (sample.getSpecial() != 1) {// 不是特价
//								for (int j = 0; j < zhubulist.size(); j++) {
//
//								}
//							} else {// 是特价
//
//							}
//						} else {// 只有一种主布
//							Double buyongliang = (Double) zhubu.get("clothNumber");
//							// 遮光里布的宽
//							zheguanglibu = (Double) zhubu.get("actualWide");
//							if (list.get(c).getBreadthStatus() == 0 && (Integer) zhubu.get("useInverte") == 1
//									|| list.get(c).getBreadthStatus() == 1 && (Integer) zhubu.get("useInverte") == 0) {
//								Double shupin = priceDao.selectshupinsum(goods.getSampleId());
//								zheguanglibu = zheguanglibu + shupin;
//							}
//							// 判断是否是特价
//							if (sample.getSpecial() != 1) {// 不是特价
//								if (goods.getStyle() == 0) {// 判断是打孔还是韩折 打孔
//									goods.setSumBPrice(buyongliang * sample.getHolePrice());
//									sumPrice += buyongliang * sample.getHolePrice();
//								} else {// 韩折
//									goods.setSumBPrice(buyongliang * sample.getZhePrice());
//									sumPrice += buyongliang * sample.getZhePrice();
//								}
//							} else {// 是特价
//								if (goods.getStyle() == 0) {// 判断是打孔还是韩折 打孔
//									goods.setSumBPrice(buyongliang * sample.getHoleSpecPrice());
//									sumPrice += buyongliang * sample.getHoleSpecPrice();
//								} else {// 韩折
//									goods.setSumBPrice(buyongliang * sample.getZheSpecPrice());
//									sumPrice += buyongliang * sample.getZheSpecPrice();
//								}
//							}
//							Map<String, Object> cunzhubu = new HashMap<>();
//							// 把用了几幅布添加到里面
//							cunzhubu.put("dosage", zhubu.get("dosage"));
//							// 把是否倒幅添加到里面
//							cunzhubu.put("useInverte", zhubu.get("useInverte"));
//							// 把型号添加到里面
//							cunzhubu.put("serialnumber", zhubu.get("serialnumber"));
//							// 把用量添加到里面
//							cunzhubu.put("dosageNumber", zhubu.get("clothNumber"));
//							cunzhubu.put("specialId", special.getSpecialId());
//							cunzhubu.put("useWide", zhubu.get("actualWide"));
//							cunzhubu.put("useHigh", zhubu.get("actualHigh"));
//							// 类型 是 主布 配布 遮光里布 半遮光里布
//							cunzhubu.put("type", 0);
//							// 商品Id
//							cunzhubu.put("goodsId", goods.getGoodsId());
//							// 把主布的用量什么存到数据库
//							workerDao.insertzhubu(cunzhubu);
//							if (p) {// 有花边,把花边的用量存到数据库
//								Map<String, Object> cunhuabian = new HashMap<>();
//								cunhuabian.put("serialnumber", list.get(o).getSerialnumber());
//								cunhuabian.put("dosageNumber", zhubu.get("clothNumber"));
//								cunhuabian.put("specialId", special.getSpecialId());
//								cunhuabian.put("type", 6);
//								workerDao.insertzhubu(cunhuabian);
//							}
//							// 获得使用的配布数组
//							if (goods.getShadingCloth() == 1) {// 有遮光里布
//								Composition composition = priceDao.selectZhe();
//								Map<String, Object> zheguang = Calculate.clientPrice(1.0, sample.getHem(), zheguanglibu,
//										jsonObject.getDouble("high"), composition);
//								sumPrice = sumPrice + (Double) zheguang.get("sumPrice");
//								Map<String, Object> cunzheguang = new HashMap<>();
//								// 把用了几幅布添加到里面
//								cunzheguang.put("dosage", zheguang.get("dosage"));
//								// 把是否倒幅添加到里面
//								cunzheguang.put("useInverte", zheguang.get("useInverte"));
//								// 把型号添加到里面
//								cunzheguang.put("serialnumber", zheguang.get("serialnumber"));
//								// 把用量添加到里面
//								cunzheguang.put("dosageNumber", zheguang.get("clothNumber"));
//								cunzheguang.put("specialId", special.getSpecialId());
//								cunzheguang.put("useWide", zheguang.get("actualWide"));
//								cunzheguang.put("useHigh", zheguang.get("actualHigh"));
//								// 类型 是 主布 配布 遮光里布 半遮光里布
//								cunzheguang.put("type", 3);
//								// 商品Id
//								cunzheguang.put("goodsId", goods.getGoodsId());
//								// 把主布的用量什么存到数据库
//								workerDao.insertzhubu(cunzheguang);
//
//							} else if (goods.getShadingCloth() == 2) {// 有半遮光里布
//								Composition composition = priceDao.selectBanzhe();
//								Map<String, Object> banzhe = Calculate.clientPrice(1.0, sample.getHem(), zheguanglibu,
//										jsonObject.getDouble("high"), composition);
//								sumPrice = sumPrice + (Double) banzhe.get("sumPrice");
//								Map<String, Object> cunbanzheguang = new HashMap<>();
//								// 把用了几幅布添加到里面
//								cunbanzheguang.put("dosage", banzhe.get("dosage"));
//								// 把是否倒幅添加到里面
//								cunbanzheguang.put("useInverte", banzhe.get("useInverte"));
//								// 把型号添加到里面
//								cunbanzheguang.put("serialnumber", banzhe.get("serialnumber"));
//								// 把用量添加到里面
//								cunbanzheguang.put("dosageNumber", banzhe.get("clothNumber"));
//								cunbanzheguang.put("specialId", special.getSpecialId());
//								cunbanzheguang.put("useWide", banzhe.get("actualWide"));
//								cunbanzheguang.put("useHigh", banzhe.get("actualHigh"));
//								// 类型 是 主布 配布 遮光里布 半遮光里布
//								cunbanzheguang.put("type", 4);
//								// 商品Id
//								cunbanzheguang.put("goodsId", goods.getGoodsId());
//								// 把主布的用量什么存到数据库
//								workerDao.insertzhubu(cunbanzheguang);
//							}
//							@SuppressWarnings("unchecked")
//							List<Map<String, Object>> peibulist = (List<Map<String, Object>>) zhubu.get("peibuList");
//							if (peibulist != null && peibulist.size() > 0) {
//								for (int a = 0; a < peibulist.size(); a++) {
//									Map<String, Object> cunpeibu = new HashMap<>();
//									cunpeibu.put("dosage", peibulist.get(a).get("dosage"));
//									cunpeibu.put("useInverte", peibulist.get(a).get("useInverte"));
//									cunpeibu.put("serialnumber", peibulist.get(a).get("serialnumber"));
//									cunpeibu.put("dosageNumber", peibulist.get(a).get("clothNumber"));
//									cunpeibu.put("type", 1);
//									cunpeibu.put("specialId", special.getSpecialId());
//									cunpeibu.put("useWide", peibulist.get(a).get("actualWide"));
//									cunpeibu.put("useHigh", peibulist.get(a).get("actualHigh"));
//									cunpeibu.put("goodsId", goods.getGoodsId());
//									// 把用量什么存到数据库
//									workerDao.insertzhubu(cunpeibu);
//								}
//							}
//						}
//
//					}
//				}
//				if (shamishu != null) {
//					// 不为空证明有纱
//					JSONArray jsonArray1 = JSONArray.parseArray(shamishu);
//					for (int a = 0; a < jsonArray1.size(); a++) {
//						JSONObject jsonObject = (JSONObject) jsonArray1.get(a);
//						// 存到数据库中并获得id
//						Goods_special special = new Goods_special();
//						// 纱配件
//						if (goods.getInstallCase() == null || goods.getInstallCase() == 0) {
//							Goods pei = new Goods();
//							pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
//							pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
//							pei.setPinstallSite(jsonObject.getString("pinstallSite"));
//							pei.setNumber(1);
//							pei.setPanzhuangfangshi(jsonObject.getString("panzhuangfangshi"));
//							pei.setAcc(jsonObject.getString("acc"));
//							workerDao.insertpeijian(pei);
//							special.setAccId(pei.getGoodsId());
//						}
//						special.setInstallSite(jsonObject.getString("installSite"));
//						special.setAccessoriesType(jsonObject.getString("accessoriesType"));
//						special.setWindow(jsonObject.getString("window"));
//						special.setWide(jsonObject.getDouble("wide"));
//						special.setHigh(jsonObject.getDouble("high"));
//						special.setProduction(jsonObject.getInteger("production"));
//						special.setType(1);
//						special.setGoodsId(goods.getGoodsId());
//						int goodsIdChild = goods.getGoodsId() * 10 + a;
//						special.setGoodsIdChild(goodsIdChild);
//						special.setWhatStructure(1);
//						workerDao.insertSpecial(special);
//						JSONArray imgs = (JSONArray) jsonObject.get("imgs");
//						for (int s = 0; s < imgs.size(); s++) {
//							// 把图片存到图片表里
//							Map<String, Object> imgmap = new HashMap<>();
//							imgmap.put("specialId", special.getSpecialId());
//							imgmap.put("img", imgs.get(s));
//							imgmap.put("paizhaoType", 0);
//							workerDao.insertImg(imgmap);
//						}
//						Composition sha = workerDao.selectSha(sample.getSampleId());
//						Map<String, Object> shamap = Calculate.clientPrice(sample.getTimes_fold(), sample.getHem(),
//								jsonObject.getDouble("wide"), jsonObject.getDouble("high"), sha);
//						sumPrice = sumPrice + (Double) shamap.get("sumPrice");
//						goods.setSumSPrice((Double) shamap.get("sumPrice"));
//						Map<String, Object> cunsha = new HashMap<>();
//						// 把用了几幅布添加到里面
//						cunsha.put("dosage", shamap.get("dosage"));
//						// 把是否倒幅添加到里面
//						cunsha.put("useInverte", shamap.get("useInverte"));
//						// 把型号添加到里面
//						cunsha.put("serialnumber", shamap.get("serialnumber"));
//						// 把用量添加到里面
//						cunsha.put("dosageNumber", shamap.get("clothNumber"));
//						cunsha.put("useWide", shamap.get("actualWide"));
//						cunsha.put("specialId", special.getSpecialId());
//						cunsha.put("useHigh", shamap.get("actualHigh"));
//						// 类型 是 主布 配布 遮光里布 半遮光里布
//						cunsha.put("type", 5);
//						// 商品Id
//						cunsha.put("goodsId", goods.getGoodsId());
//						// 把主布的用量什么存到数据库
//						workerDao.insertzhubu(cunsha);
//
//					}
//				}
//				if (toumishu != null) {
//					goods.setShadeHead(1);
//					// 不为空证明有帘头
//					JSONArray jsonArray1 = JSONArray.parseArray(toumishu);
//					for (int a = 0; a < jsonArray1.size(); a++) {
//						JSONObject jsonObject = (JSONObject) jsonArray1.get(a);
//						// 查询样品中帘头的价格
//						Double liantou = workerDao.selectLiantou(sample.getSampleId());
//						sumPrice += jsonObject.getDouble("wide") * liantou;
//						goods.setSumLPrice(jsonObject.getDouble("wide") * liantou);
//						// 帘头配件
//						Goods pei = new Goods();
//						pei.setAccessoriesType(jsonObject.getString("accessoriesType"));
//						pei.setSaleNumber(jsonObject.getDouble("saleNumber"));
//						pei.setAcc(jsonObject.getString("acc"));
//						workerDao.insertpeijian(pei);
//						// 存到数据库中并获得id
//						Goods_special special = new Goods_special();
//						special.setWide(jsonObject.getDouble("wide"));
//						special.setAccId(pei.getGoodsId());
//						special.setInstallSite(jsonObject.getString("installSite"));
//						special.setType(2);
//						special.setGoodsId(goods.getGoodsId());
//						int goodsIdChild = goods.getGoodsId() * 10 + a;
//						special.setGoodsIdChild(goodsIdChild);
//						special.setWhatStructure(3);
//						workerDao.insertSpecial(special);
//						if ("不满墙".equals(jsonObject.getString("installSite"))) {
//							// num为图片地址
//							// 把图片存到图片表里
//							Map<String, Object> imgmap = new HashMap<>();
//							imgmap.put("specialId", special.getSpecialId());
//							imgmap.put("img", jsonObject.getString("num"));
//							imgmap.put("paizhaoType", 0);
//							workerDao.insertImg(imgmap);
//						}
//						// 把帘头用量存到数据库
//						Map<String, Object> cuntou = new HashMap<>();
//						// 把用了几幅布添加到里面
//						cuntou.put("dosage", 1);
//						// 把是否倒幅添加到里面
//						cuntou.put("useInverte", 0);
//						// 把型号添加到里面
//						cuntou.put("serialnumber", list.get(linantou).getSerialnumber());
//						// 把用量添加到里面
//						cuntou.put("dosageNumber", jsonObject.getDouble("wide"));
//						cuntou.put("useWide", 0);
//						cuntou.put("specialId", special.getSpecialId());
//						cuntou.put("useHigh", 0);
//						// 类型 是 主布 配布 遮光里布 半遮光里布
//						cuntou.put("type", 2);
//						// 商品Id
//						cuntou.put("goodsId", goods.getGoodsId());
//						// 把主布的用量什么存到数据库
//						workerDao.insertzhubu(cuntou);
//					}
//				}
//				goods.setSumPrice(sumPrice);
//				goods.setIstianxie(1);
//				// 把商品存到数据库中
//				workerDao.updateGoods(goods);
//			} else {// 成品帘
//				parseAndAddFinishShade(finishShade, goods);
//			}
//		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 解析并添加布
	// 解析并添加纱
	// 解析并添加帘头
	// 解析并添加成品帘
	public void parseAndAddFinishShade(String finishShade, Goods goods) {
		JSONArray jsonArray = JSONArray.parseArray(finishShade);
		
		Double sumPrice = 0.0;
		Goods goodsList;
		Map<String, Object> finishList;
		Goods price;
		Double curtainPrice;
		Map<String, Object> parameter = new HashMap<>();
		
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = (JSONObject) jsonArray.get(i);
			// 成品帘配件
			Goods pei = new Goods();
			pei.setPinstallSite(jsonObject.getString("pinstallSite"));
			pei.setSaleNumber(jsonObject.getDouble("wide"));
			pei.setNumber(1);//成品帘配件默认为单杆
			workerDao.insertpeijian(pei);
			Goods_special special = new Goods_special();
			special.setAccId(pei.getGoodsId());
			special.setInstallSite(jsonObject.getString("installSite"));
//			special.setAccessoriesType(jsonObject.getString("accessoriesType"));
			//因不明确成品帘到底用什么配件，且顶装配件和侧装配件定义太模糊，
			// 产品未指定成品帘配件类型，故这里先都设定为罗马杆，不然无法计算安装费
			special.setAccessoriesType("罗马杆");
			special.setWindow(jsonObject.getString("window"));
			special.setWide(jsonObject.getDouble("wide"));
			special.setHigh(jsonObject.getDouble("high"));
			special.setType(3);
			special.setGoodsId(goods.getGoodsId());
			int goodsIdChild = goods.getGoodsId() * 10 + i;
			special.setGoodsIdChild(goodsIdChild);
			special.setRemark(jsonObject.getString("remark"));
			special.setWhatStructure(4);// 自定成品帘配件为4
			workerDao.insertSpecial(special);
			JSONArray imgsB = (JSONArray) jsonObject.get("imgs");
			for (int s = 0; s < imgsB.size(); s++) {
				// 把图片存到图片表里
				Map<String, Object> imgmap = new HashMap<>();
				imgmap.put("specialId", special.getSpecialId());
				imgmap.put("img", imgsB.get(s));
				imgmap.put("paizhaoType", 0);
				workerDao.insertImg(imgmap);
			}
			
			//计算成品帘价格
//			if (goods != null && goods.getSampleId() != null){
//				parameter.put("sampleId", goods.getSampleId());
//				ArrayList<Map<String, Object>> list = new ArrayList<>();
//				Map<String, Object> childParameter = new HashMap<>();
//				childParameter.put("wide", jsonObject.getDouble("wide"));
//				childParameter.put("high", jsonObject.getDouble("high"));
//				list.add(childParameter);
//				parameter.put("finish", list);
//				curtainPrice = (Double)(priceService.getPriceOfFinish(new Gson().toJson(parameter)).getData());
//				sumPrice += curtainPrice;
//			}
		}
		goods.setSumPrice(sumPrice);
		goods.setIstianxie(1);
		workerDao.updateGoods(goods);
	}

	// 更改手机号
	public BuluResult updatePhone(HttpServletRequest request, String account) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		JSONObject object = getAccountMap(account, 2);
		String password = object.getString("password");
		String weiyi = workerDao.selectWeiyiid(workerId);
		password = MD5Utils.MD5(password + weiyi);
		Map<String, Object> map = new HashMap<>();
		map.put("workerId", workerId);
		map.put("password", password);
		Integer zhengq = workerDao.selectPZQ(map);
		BuluResult buluResult = new BuluResult();
		if (zhengq > 0) {
			// //修改手机号
			// map.put("phone", phone);
			// workerDao.updatePhone(map);
			buluResult.setStatus(0);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("输入的密码不正确!!!");
		}
		return buluResult;
	}

	// 查询头像
	public BuluResult selectTouxiang(String phone) {
		String img = workerDao.selectTouxiang(phone);
		BuluResult buluResult = new BuluResult();
		if (img != null) {
			buluResult.setData(img);
			buluResult.setStatus(0);
		} else {
			buluResult.setData("还没上传头像!");
			buluResult.setStatus(1);
		}
		return buluResult;
	}

	// 根据商品号查询商品
	public BuluResult selectGoodxinxi(Integer goodsId) {
		Map<String, Object> map = workerDao.selectGoodxinxi(goodsId);
		if ((Integer) map.get("type") == 0) {// 布艺帘
			List<Map<String, Object>> list = workerDao.selectSpecial(goodsId);
			for (int a = 0; a < list.size(); a++) {
				List<String> imgs = workerDao.selectImgs((Integer) list.get(a).get("specialId"));
				list.get(a).put("imgs", imgs);
				// 获得安装位置拍照
				List<String> installImgs = workerDao.selectInstallImgs((Integer) list.get(a).get("specialId"));
				list.get(a).put("installImgs", installImgs);
			}
			map.put("specials", list);
		} else {// 成品帘
			// 根据goodsId查找旗下的所有special参数
			List<Map<String, Object>> list = workerDao.selectFinishSpecial(goodsId);
			for (int a = 0; a < list.size(); a++) {
				// 查找该special下的照片
				List<String> imgs = workerDao.selectChengpinImgs((Integer) list.get(a).get("specialId"));
				list.get(a).put("imgs", imgs);
				// 获得安装位置拍照
				List<String> installImgs = workerDao.selectInstallImgs((Integer) list.get(a).get("specialId"));
				list.get(a).put("installImgs", installImgs);
				Goods goods = workerDao.selectGoodsInfo((Integer) list.get(a).get("accId"));
				list.get(a).put("pinstallSite", goods.getPinstallSite());
			}
			map.put("specials", list);
			List<String> installImgs = workerDao.selectImgsCplInstall((Integer) map.get("goodsId"));
			map.put("installImgs", installImgs);
			// ----------------
			/*
			 * List<String> imgs = workerDao.selectImgsCpl((Integer)
			 * map.get("goodsId")); map.put("measureImgs", imgs); List<String>
			 * installImgs = workerDao.selectImgsCplInstall((Integer)
			 * map.get("goodsId")); map.put("installImgs", installImgs);
			 */
		}

		if (map.get("locationId") != null) {
			String locationContent = workerDao.selectLocationConent((Integer) map.get("locationId"));
			map.put("locationContent", locationContent);
		} else {
			map.put("locationContent", map.get("otherRoom"));
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	// 测量填写信息查询布特殊图
	public BuluResult selectTeshu() {
		List<Map<String, Object>> list = workerDao.selectTeshu();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

	// 查询广告
	public BuluResult selectGuanggao() {
		List<String> guanggao = workerDao.selectGuanggao();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(guanggao);
		return buluResult;
	}

	// 意见反馈
	public BuluResult insertWenti(HttpServletRequest request, String problemCount, String imgs) {
		String sign = (String) request.getParameter("sign");
		if (sign != null) {
			Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
			Problem problem = new Problem();
			problem.setProblemCount(problemCount);
			problem.setTypeId(workerId);
			problem.setAppId(2);
			Date problemTime = new Date();
			problem.setProblemTime(problemTime);
			workerDao.insertProblem(problem);
			if (imgs != null) {
				JSONArray jsonimgs = JSONArray.parseArray(imgs);
				for (int a = 0; a < jsonimgs.size(); a++) {
					Map<String, Object> map = new HashMap<>();
					map.put("img", jsonimgs.get(a));
					map.put("problemId", problem.getProblemId());
					workerDao.insertWenti(map);
				}
			}
		} else {
			Problem problem = new Problem();
			problem.setProblemCount(problemCount);
			problem.setAppId(2);
			Date problemTime = new Date();
			problem.setProblemTime(problemTime);
			workerDao.insertProblem(problem);
			if (imgs != null) {
				JSONArray jsonimgs = JSONArray.parseArray(imgs);
				for (int a = 0; a < jsonimgs.size(); a++) {
					Map<String, Object> map = new HashMap<>();
					map.put("img", jsonimgs.get(a));
					map.put("problemId", problem.getProblemId());
					workerDao.insertWenti(map);
				}
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询帘头图片
	public BuluResult selectSheadheadImg() {
		List<String> list = workerDao.selectSheadheadImg();
		logger.error(JSONObject.toJSON(list));
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(list);
		return buluResult;
	}

	// 根据sampleId查询宽高限制
	public BuluResult selectXianzhi(Integer sampleId) {
		Map<String, Object> map = workerDao.selectXianzhi(sampleId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(map);
		return buluResult;
	}

	// 订单中添加商品
	public BuluResult insertGoods(Integer indentId, Goods goods) {
		// 由于家居版下的单子是包含商品的，并且有sampleId和customerId，当工人接到单子后，如果将已有商品删除的话重新添加后
		// 将不会有sampleId和customerId，故这里应该将家居过来的商品的sampleId和customerId查出来重新赋给工人添加的商品
		Map<String,Object> param = new HashMap<>();
		param.put("indentId",indentId);
		List<Map<String, Object>> goodsList = workerDao.selectGoods(param);
		if (goodsList.size() > 0) {
			Integer sampleId = (Integer) goodsList.get(0).get("sampleId");
			Integer customerId = (Integer) goodsList.get(0).get("customerId");
//			goods.setSampleId(sampleId);
			goods.setCustomerId(customerId);
		}
		// 把goods存到商品表中并获取id
		workerDao.insertGoods(goods);
		// 把商品号与订单号关联一下
		Map<String, Object> send = new HashMap<>();
		send.put("goodsId", goods.getGoodsId());
		send.put("indentId", indentId);
		workerDao.insertindentGoods(send);
		//更新测量房间的个数
		Integer distinct=workerDao.selectDistinct(indentId)==null?0:workerDao.selectDistinct(indentId);
		Integer otherRoom=workerDao.selectOtherRooms(indentId)==null?0:workerDao.selectOtherRooms(indentId);
		Integer roomNumB =distinct+otherRoom;
		Map<String, Object>updateMap=new HashMap<String, Object>();
		updateMap.put("indentId", indentId);
		updateMap.put("roomNumB", roomNumB);
		workerDao.updateIndentRoom(updateMap);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 删除订单中商品
	public BuluResult deleteGoods(Integer goodsId) {
		// JSONArray goodsIdD = JSONObject.parseArray(goodsIds);
		// for(int a=0;a<goodsIdD.size();a++){
		// Integer goodsId = (Integer)goodsIdD.get(a);
		Goods_indent goods_indent=workerDao.selectIndentIdByGoodsId(goodsId);
		workerDao.deleteGoods(goodsId);
		// 删除商品跟订单的关系
		workerDao.deleteIndentGoods(goodsId);
		// }
		//更新测量房间的个数
		Integer distinct=workerDao.selectDistinct(goods_indent.getIndentId())==null?0:workerDao.selectDistinct(goods_indent.getIndentId());
		Integer otherRoom=workerDao.selectOtherRooms(goods_indent.getIndentId())==null?0:workerDao.selectOtherRooms(goods_indent.getIndentId());
		Integer roomNumB =distinct+otherRoom;
		Map<String, Object>updateMap=new HashMap<String, Object>();
		updateMap.put("indentId", goods_indent.getIndentId());
		updateMap.put("roomNumB", roomNumB);
		workerDao.updateIndentRoom(updateMap);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 添加安装图片
	public BuluResult insertATupian(String specialImgs) {
		JSONArray specialImg = JSONObject.parseArray(specialImgs);
		for (int b = 0; b < specialImg.size(); b++) {
			JSONObject specialId2 = specialImg.getJSONObject(b);
			Integer specialId = specialId2.getInteger("specialId");
			JSONArray jsonArray = specialId2.getJSONArray("imgs");
			for (int a = 0; a < jsonArray.size(); a++) {
				Map<String, Object> map = new HashMap<>();
				map.put("specialId", specialId);
				map.put("img", jsonArray.get(a));
				map.put("paizhaoType", 1);
				workerDao.insertTupian(map);
			}
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 添加维修图片
	public BuluResult insertWTupian(Integer specialId, String imgs) {
		JSONArray jsonArray = JSONObject.parseArray(imgs);
		for (int a = 0; a < jsonArray.size(); a++) {
			Map<String, Object> map = new HashMap<>();
			map.put("specialId", specialId);
			map.put("img", jsonArray.get(a));
			map.put("paizhaoType", 2);
			workerDao.insertTupian(map);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 消息中心
	public BuluResult selectMessage(Integer page) {
		page = 10 * (page - 1);
		List<Map<String, Object>> list = workerDao.selectMessage(page);
		BuluResult buluResult = new BuluResult();
		if (list != null && list.size() > 0) {
			buluResult.setStatus(0);
			buluResult.setData(list);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("暂无数据!!!");
		}
		return buluResult;
	}

	// 更改手机号
	public BuluResult updatePhone2(HttpServletRequest request, String phone) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Map<String, Object> map = new HashMap<>();
		map.put("workerId", workerId);
		map.put("phone", phone);
		// 修改手机号
		workerDao.updatePhone(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询银行卡
	public BuluResult selectBank(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		List<Map<String, Object>> list = workerDao.selectBank(workerId);
		BuluResult buluResult = new BuluResult();
		if (list != null && list.size() > 0) {
			buluResult.setStatus(0);
			buluResult.setData(list);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("暂无银行卡数据!!!");
		}
		return buluResult;
	}

	// 添加银行卡
	public BuluResult insertBank(HttpServletRequest request, String boundBank, String bankNumber) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		workerDao.updateDefult(workerId);
		Map<String, Object> map = new HashMap<>();
		map.put("peopleId", workerId);
		map.put("boundBank", boundBank);
		map.put("bankNumber", bankNumber);
		workerDao.insertBank(map);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 设为默认银行卡
	public BuluResult updateBank(HttpServletRequest request, Integer bankCardId) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		workerDao.updateDefult(workerId);
		// 设为默认
		workerDao.updateBank(bankCardId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 删除银行卡
	public BuluResult deleteBank(Integer bankCardId) {
		workerDao.deleteBank(bankCardId);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询版权颜色
	public BuluResult selectCopyright() {
		String copyright = workerDao.selectCopyright();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(copyright);
		return buluResult;
	}

	// 查询说明
	public BuluResult selectAboutMe() {
		String aboutMe = workerDao.selectAboutMe();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(aboutMe);
		return buluResult;
	}

	// 查询软件使用许可协议
	public BuluResult selectVersionIntroduce() {
		String versionIntroduce = workerDao.selectVersionIntroduce();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(versionIntroduce);
		return buluResult;
	}

	// 查询平台服务协议
	public BuluResult selectAgreement() {
		String agreement = workerDao.selectAgreement();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(agreement);
		return buluResult;
	}

	// 添加成品帘安装图片
	public BuluResult insertInstallImgs(Integer goodsId, String installImgs) {
		JSONArray jsonArray = JSONObject.parseArray(installImgs);
		for (int a = 0; a < jsonArray.size(); a++) {
			Map<String, Object> map = new HashMap<>();
			map.put("goodsId", goodsId);
			map.put("img", jsonArray.getString(a));
			workerDao.insertInstallImgs(map);
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 验证码验证成功之后改变订单状态 并给工人钱包加钱
	public BuluResult updateIndent(HttpServletRequest request, Integer indentId) {
		BuluResult buluResult = new BuluResult();
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 修改订单状态为已完成
		logger.error("=============更改的订单状态已完成" + indentId + "==========");
		workerDao.updateIndentComplete(indentId);
		// 获取订单金额并转给工人(包含customerId)
		Map<String, Object> map = workerDao.selectIndentPrice(indentId);
		// 获取工人钱包金额
		Double wallet = workerDao.selectWallet(workerId);
		wallet = Arith.add(wallet, Arith.add((Double) map.get("indentPrice"), (Double) map.get("addPrice")));
		Map<String, Object> send = new HashMap<>();
		send.put("workerId", workerId);
		send.put("wallet", wallet);
		int result = workerDao.updateWalletComple(send);
		// 保存改条钱包交易记录
		/*
		 * if(result>0){ walletRecordServie.insertWalletRecord(workerId,
		 * indentId, 2, (Double) map.get("indentPrice"), (Double)
		 * map.get("addPrice"),null, Arith.add((Double) map.get("indentPrice"),
		 * (Double) map.get("addPrice")), null, 5, 1, "工人完成订单", 0, "钱包余额交易"); }
		 */
		// 保存工人钱包变动记录
		/*
		 * workerId, indentId, 2, mainMoney, addMoney, subMoney, finalGetMoney,
		 * finalLostMoney, walletRecharge, walletWithdrawals, type, status,
		 * remark, isDelete, method }
		 */
		if (result > 0) {
			workerWalletRecordService.insertWorkerWalletRecord(workerId, indentId, 2, (Double) map.get("indentPrice"),
					(Double) map.get("addPrice"), null,
					Arith.add((Double) map.get("indentPrice"), (Double) map.get("addPrice")), null, null, null,
					WorkerType.COMPLETE.getIndex(), 1, "工人完成已接订单", 0, "钱包余额交易");
		}
		String alias = "尊敬的客户:";
		String content = "你有一个订单已经完成,赶快去查看吧!!!";
		// sendPushMessage(String alias, String content, Map<String, String>
		// msgType)
		Map<String,Object> map2 = workerDao.selectIndent(indentId);
		if (map2.get("type") != null && 2 != ((Integer)map2.get("type"))){//type为2是维修单，维修单没有商品，无需计算
			List<Goods> goodss = workerDao.getGoodByIndentId(workerDao.getGoodIdByIndentId(indentId));
			// 获取订单下的所有商品，计算价格，并存入goods中
			for (Goods goods : goodss) {
				if (goods.getSampleId() != null && !"".equals(goods.getSampleId())){
					if (goods.getType() == 0 || goods.getType() == 1) {
						Map<String, Object> map1 = new HashMap<>();
						map1.put("goodsId", goods.getGoodsId());
						map1.put("customerId", map.get("customerId"));
						Map<String, Object> update = new HashMap<>();
						Goods goodsList;
						Map<String, Object> finishList;
						Goods price = new Goods();
						Object curtainPrice = new Double("0.0");
						Map<String, Object> parameter = new HashMap<>();
						if (goods.getType() == 1) {//成品帘
							//{"sampleId":1002,"finish":[{"wide":"2","high":"2"}]}
							try{
								finishList = workerDao.getGoodsToCaclCurtainPriceByGoodsId(goods.getGoodsId());
								parameter.put("sampleId", finishList.get("sampleId"));
								ArrayList<Map<String, Object>> list = new ArrayList<>();
								Map<String, Object> childParameter = new HashMap<>();
								childParameter.put("wide", finishList.get("wide"));
								childParameter.put("high", finishList.get("high"));
								list.add(childParameter);
								parameter.put("finish", list);
								curtainPrice = priceService.getPriceOfFinish(new Gson().toJson(parameter)).getData();
								System.err.println("工人端完成订单时计算的成品帘价格==>"+curtainPrice);

							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							}

							update.put("goodsId", goods.getGoodsId());
							update.put("goodsPrice", (Double)curtainPrice);
						}else if (goods.getType() == 0){//布艺帘
							goodsList = workerDao.getGoodsToCaclPriceByGoodsId(map1);
							try {
								price = (Goods) priceService.calcCurtainPrice(goodsList).getData();
								System.err.println("工人端完成订单时计算的布艺帘价格==>"+price);
							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							}
							Double sumPrice = price.getGoodsPrice();
							Double sumBPrice = price.getSumBPrice();
							Double sumShadingBprice = price.getSumShadingBprice();
							Double sumSPrice = price.getSumSPrice();
							Double sumLPrice = price.getSumLPrice();
							update.put("goodsId", goods.getGoodsId());
							update.put("goodsPrice", sumPrice);
							update.put("sumBPrice", sumBPrice);
							update.put("sumShadingBprice", sumShadingBprice);
							update.put("sumSPrice", sumSPrice);
							update.put("sumLPrice", sumLPrice);
						}
						workerDao.updateGoodByGoodId(update);
					}
				}
			}
		}

		buluResult.setStatus(0);
		return buluResult;
	}

	// 申请提现
	public BuluResult submitWithdrawal(HttpServletRequest request, Double money, Integer bankCardId) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 类型是工人
		MoneyRecord moneyRecord = new MoneyRecord();
		moneyRecord.setPeopleId(workerId);
		moneyRecord.setType(1);
		moneyRecord.setMoney(money);
		Date date = new Date();
		moneyRecord.setRecordTime(date);
		moneyRecord.setStatus(0);
		moneyRecord.setWithdrawalStatus(0);
		// 根据银行卡id查询银行卡名和银行卡号
		Map<String, Object> map = workerDao.selectBankCardOne(bankCardId);
		moneyRecord.setBoundBank((String) map.get("boundBank"));
		moneyRecord.setBankNumber((String) map.get("bankNumber"));
		// 插入到数据库中
		workerDao.insertMoneyRecord(moneyRecord);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询个人信息
	public BuluResult selectWorker(HttpServletRequest request) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		long time = 2592000000l;// 30天的毫秒值
		Worker worker = workerDao.selectWorker(workerId);
		String city = workerDao.selectKoukuan(workerId);
		// 上次修改工作区域的时间，为null时证明还没有修改过
		String lastChangeTime = workerDao.selectWorkerChangeAreaInfo(workerId);
		if (lastChangeTime == null || lastChangeTime.length() <= 0) {// 没有修改过
			worker.setIsCanChange(String.valueOf(1));
		} else {
			long timeTemp = System.currentTimeMillis() - Long.parseLong(lastChangeTime);
			if (timeTemp > time) {// 距离上次修改时间大于30天
				worker.setIsCanChange(String.valueOf(1));
			} else {// 距离上次修改时间小于30天，不可以修改
				worker.setIsCanChange(String.valueOf(0));
			}
		}

		// 根据工人id查询工作区
		List<Map<String, String>> workerSpaces = workerDao.selectWorkerSpace(workerId);

		worker.setWorkerSpaces(workerSpaces);

		// 根据工人所在城市查询该城市所有区
		List<Map<String, String>> area = workerDao.selectArea(city);
		Iterator<Map<String, String>> iterator = area.iterator();
		while (iterator.hasNext()) {
			Map<String, String> next = iterator.next();

			if ("其他区".equals((String) next.get("name"))) {
				iterator.remove();
			}
		}

		worker.setArea(area);
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData(worker);
		return buluResult;
	}

	// 修改工作区域
	public BuluResult updateWorkerSpaces(HttpServletRequest request, String workerSpaces) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 更新工人修改工作区域的时间
		Map<String, Object> m = new HashMap<>();
		m.put("lastChangeAreaTime", String.valueOf(System.currentTimeMillis()));
		m.put("workerId", workerId);
		workerDao.updateWorkerChangeAreaInfo(m);
		// 根据id把原来的工作区域删除
		workerDao.deleteWorkerSpaces(workerId);
		// 把新的工作区域加入到表中
		JSONArray jsonArray = JSONObject.parseArray(workerSpaces);
		for (int a = 0; a < jsonArray.size(); a++) {
			JSONObject object = jsonArray.getJSONObject(a);
			Map<String, Object> map = new HashMap<>();
			map.put("workerId", workerId);
			map.put("workerSpace", object.get("workerSpace"));
			map.put("workerSpaceCode", object.get("workerSpaceCode"));
			workerDao.insertWorkerSpace(map);// 再添加新的区域
		}
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData("工作区域变更成功");
		return buluResult;
	}

	// 修改工人地址{未完待续2017-05-04-23:07}
	public BuluResult updateWorkerAddress(HttpServletRequest request, String province, String city, String area) {
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));

		String provinceCode = workerDao.selectProvinceCodeForWorker(province);
		String cityCode = workerDao.selectCityCodeForWorker(city);
		String workerSpaceCodeStr = provinceCode + "/" + cityCode;

		// 更新工人修改工作区域的时间
		Map<String, Object> m = new HashMap<>();
		m.put("lastChangeAreaTime", String.valueOf(System.currentTimeMillis()));
		m.put("workerId", workerId);
		m.put("workerSpace", province + city);
		m.put("provinceCode", provinceCode);
		m.put("cityCode", cityCode);
		m.put("workerSpaceCodeStr", workerSpaceCodeStr);
		workerDao.updateWorkerChangeAddressInfo(m);
		// 根据id把原来的工作区域删除
		workerDao.deleteWorkerSpaces(workerId);

		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		buluResult.setData("地址变更成功");
		return buluResult;
	}

	// 工人 充值
	public BuluResult submitRecarge(HttpServletRequest request, Double money, Integer cashInter) {
		logger.error("=======================================工人开始充值生成支付宝请求参数=========================================");
		BuluResult buluResult = new BuluResult();
		// 返回确认充值的订单参数
		String orderInfo = "";
		if (money == 0.00 || money < 0.00) {
			buluResult.setStatus(1);
			buluResult.setMessage("充值金额输入有误，请重新输入!");
			return buluResult;
		}
		// 获取工人在worker表中的唯一id
		String sign = request.getParameter("sign");
		Integer peopleId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 充值参数
		WorkerWalletRecord workerWalletRecord = new WorkerWalletRecord();
		workerWalletRecord.setWorkerId(peopleId);
		workerWalletRecord.setWorkerType(2);
		workerWalletRecord.setType(WorkerType.RECHARGE.getIndex());
		workerWalletRecord.setOrderNo(LuKeOrderNoUtil.getWorkerOrderNo(peopleId, PayMethod.ALIPAY.getIndex(),
				TypeOfIndent.RECHARGE.getIndex()));
		workerWalletRecord.setStatus(0);
		workerWalletRecord.setRecordBeginTime(DateUtils.getDateTime());
		workerWalletRecord.setWalletRecharge(money);
		/*
		 * // 充值参数 Recharge recharge = new Recharge();
		 * recharge.setUserId(peopleId); recharge.setPeopleType(1);// 1代表工人
		 * recharge.setMoney(money); recharge.setCashType("工人充值");
		 * recharge.setBeginTime(new Date()); recharge.setCashInter(cashInter);
		 * recharge.setStatus(0);// 插入充值记录，先不激活0代表充值失败 String outNum = peopleId
		 * + "_" + (new Date().getTime()); recharge.setRechargeId(outNum);//
		 * 充值流水账号
		 */
		if (null == cashInter) {
			buluResult.setStatus(1);
			buluResult.setMessage("请先选择充值方式!");
			return buluResult;
		} else if (cashInter == 0) {// 0工人支付宝充值
			// 支付宝充值
			workerWalletRecord.setRemark("工人充值");
			workerWalletRecord.setFinalGetMoney(money);
			workerWalletRecord.setMethod(PayMethod.ALIPAY.getMethod());
			// 插入数据库并且将充值状态设置为0,0代表失败(在充值成功以后更改状态)
			/* rechargeDao.insertRecharge(recharge); */
			Map<String, Object> map = new HashMap<String, Object>();
			Integer walletRecordId = workerWalletRecordService.insertWorkerWalletRecord(workerWalletRecord);
			map.put("type", 5);
			map.put("walletRecordId", walletRecordId);
			map.put("recharge", "workerRecharge");
			orderInfo = AlipayParams.getAlipayParams(workerWalletRecord.getOrderNo(), money, null, "工人充值", "工人充值",
					JSONObject.toJSONString(map));
		} else if (cashInter == 1) {// 1工人微信支付
			/* rechargeDao.insertRecharge(workerWalletRecord); */
			// 微信回调参数先不写
		}
		buluResult.setStatus(0);
		buluResult.setData(orderInfo);
		return buluResult;
	}

	/**
	 * 工人提现到支付宝
	 */
	@Override
	public BuluResult alipyWithdrawal(HttpServletRequest request, Double money, String zhifubao, String name,
			String /* password */ account, Integer status) {
		logger.error("=======================================工人提现到支付宝开始=========================================");
		// 返回参数
		BuluResult buluResult = new BuluResult();
		// 提取工人id
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		// 比较输入金额和钱包
		Worker worker = workerDao.selectWorker(workerId);
		JSONObject object = getAccountMap(account, 2);
		String password = object.getString("password");
		buluResult = checkWithdrawalParams(money, zhifubao, name, password, worker);
		if (buluResult.getStatus() == 1) {
			return buluResult;
		}
		// 钱包提现到支付宝
		WorkerWalletRecord workerWalletRecord = new WorkerWalletRecord();
		workerWalletRecord.setWorkerId(workerId);
		workerWalletRecord.setWorkerType(2);
		workerWalletRecord.setType(WorkerType.WITHDRAWALS.getIndex());
		workerWalletRecord.setWalletWithdrawals(money);
		workerWalletRecord.setRecordBeginTime(DateUtils.getDateTime());
		// 商户转账唯一订单号
		workerWalletRecord.setOrderNo(LuKeOrderNoUtil.getWorkerOrderNo(workerId, PayMethod.BALABCEPAY.getIndex(),
				TypeOfIndent.WITHDRAWALS.getIndex()));
		/*
		 * // 类型是工人 MoneyRecord moneyRecord = new MoneyRecord();
		 * moneyRecord.setPeopleId(workerId); moneyRecord.setType(1);
		 * moneyRecord.setMoney(money); Date date = new Date();
		 * moneyRecord.setRecordTime(date); moneyRecord.setStatus(status); //
		 * 商户转账唯一订单号 String outBizNo = MD5Utils.MD5(String.valueOf(new
		 * Date().getTime() + "-" + worker.getWorkerId()));
		 * moneyRecord.setOutBizNo(outBizNo);
		 */
		if (status == 1) {// 支付宝方式
			workerWalletRecord.setMethod(PayMethod.BALABCEPAY.getMethod());
			workerWalletRecord.setRemark("支付宝提现");
			workerWalletRecord.setAlipayNum(zhifubao);
			/*
			 * moneyRecord.setZhifubao(zhifubao); moneyRecord.setName(name);
			 */
			// 调用支付宝转账接口
			buluResult = alipyFundTransToaccountTransfer(workerWalletRecord, zhifubao, money, name,
					worker.getWorkerId(), worker);
		} else {
			buluResult.setStatus(1);
			buluResult.setMessage("其他提现方式正在开通，敬请期待");
		}
		return buluResult;
	}

	/**
	 * 验证提现时参数的正确和合法性
	 * 
	 * @param money
	 * @param zhifubao
	 * @param name
	 * @param password
	 * @param worker
	 * @return
	 */
	private BuluResult checkWithdrawalParams(Double money, String zhifubao, String name, String password,
			Worker worker) {
		logger.error("=======================================工人提现前的参数检验=========================================");
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		if (worker.getWallet().compareTo(money) < 0) {
			buluResult.setStatus(1);
			buluResult.setMessage("提现金额大于钱包余额");
			return buluResult;
		}
		// 比较密码
		password = MD5Utils.MD5(password + worker.getWeiYi());
		if (!password.equals(worker.getPassword())) {
			buluResult.setStatus(1);
			buluResult.setMessage("密码不对,请重新输入");
			return buluResult;
		}
		if (!(worker.getAlipayNum().equals(zhifubao) && worker.getName().equals(name))) {
			buluResult.setStatus(1);
			buluResult.setMessage("提现人姓名或者支付宝账号不对");
			return buluResult;
		}
		return buluResult;
	}

	/**
	 * 单笔打款到某一个支付宝账户
	 * 
	 * @param workerWalletRecord
	 *            本次转账记录
	 * @param zhifubao
	 *            金额到账的目标账号
	 * @param money
	 *            转账的钱
	 * @param name
	 *            账号的名字
	 * @param workerId
	 *            转账人的id
	 * @return
	 */
	private BuluResult alipyFundTransToaccountTransfer(WorkerWalletRecord workerWalletRecord, String zhifubao,
			Double money, String name, Integer workerId, Worker worker) {
		logger.error("=======================================工人提现到支付宝请求开始=========================================");
		BuluResult buluResult = new BuluResult();
		// 实例化客户端
		AlipayClient alipayClient0 = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
				AlipayConfig.app_id, AlipayConfig.app_private_key, "json", "utf-8", AlipayConfig.alipay_public_key,
				"RSA2");
		AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
		AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
		// 鹿客帮的唯一提现流水号
		model.setOutBizNo(workerWalletRecord.getOrderNo());
		// 收款方账户类型。
		model.setPayeeType("ALIPAY_LOGONID");
		// 收款方账户。
		model.setPayeeAccount(zhifubao);
		// 转账金额
		DecimalFormat formater = new DecimalFormat("#0.#");
		formater.setRoundingMode(RoundingMode.FLOOR);
		logger.error(formater.format(Arith.mul(money, Arith.sub(1, 0.006))));
		model.setAmount(formater.format(Arith.mul(money, Arith.sub(1, 0.006))));
		// 付款方真实姓名
		model.setPayerShowName("郑州鹿客互联网科技有限公司");
		// 收款方真实姓名（最长支持100个英文/50个汉字）。
		model.setPayeeRealName(name);
		// 转账备注
		model.setRemark("工人个人提现");
		// 加入请求头
		request.setBizModel(model);
		AlipayFundTransToaccountTransferResponse response = null;
		try {
			Thread.sleep(3000);
			response = alipayClient0.execute(request);
			if (response.isSuccess()) {// 请求成功
				// 请求成功，把该次请求插入数据库
				insertMoneyRecord(workerWalletRecord, worker, 1);
				// 返回已经处理成功信息
				JSONObject jsonObject = JSONObject.parseObject(response.getBody());
				logger.error(jsonObject.get("alipay_fund_trans_toaccount_transfer_response"));
				buluResult.setStatus(0);
				buluResult.setMessage("提现成功");
				// {"msg":"Success","pay_date":"2017-04-15
				// 12:34:40","code":"10000","order_fee":"0.00","out_biz_no":"xia12345678890","order_id":"20170415110070001502310004692121","status":"SUCCESS"}
			} else {
				if (response.getCode().equals("40004") && response.getSubCode().equals("PAYER_BALANCE_NOT_ENOUGH")) {
					insertMoneyRecord(workerWalletRecord, worker, 2);
					buluResult.setStatus(2);
					buluResult.setMessage("提现申请中,请耐心等待...");
					return buluResult;
				}
				BuluResult buluResult2 = alipayFundTransOrderQuery(workerWalletRecord, worker);
				if (buluResult2.getStatus() == 0) {
					buluResult.setStatus(0);
					buluResult.setMessage("提现成功");
				} else {
					// 返回转账失败信息
					JSONObject jsonObject = JSONObject.parseObject(response.getBody());
					logger.error(jsonObject.get("alipay_fund_trans_toaccount_transfer_response"));
					buluResult.setStatus(1);
					buluResult.setMessage(response.getSubMsg());
				}
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			logger.error("支付宝请求暂停3秒==========================");
			e.printStackTrace();
		}
		return buluResult;
	}

	/**
	 * 提现业务的防止幂等现象
	 *
	 */
	private BuluResult alipayFundTransOrderQuery(WorkerWalletRecord workerWalletRecord, Worker worker) {
		logger.error("=======================================工人提现查看是否出现幂等现象=========================================");
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
				AlipayConfig.app_id, AlipayConfig.app_private_key, "json", "utf-8", AlipayConfig.alipay_public_key,
				"RSA2");
		BuluResult buluResult = new BuluResult();
		AlipayFundTransOrderQueryRequest request = new AlipayFundTransOrderQueryRequest();
		AlipayFundTransOrderQueryModel model = new AlipayFundTransOrderQueryModel();
		model.setOutBizNo(workerWalletRecord.getOrderNo());
		request.setBizModel(model);
		AlipayFundTransOrderQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
			if (response.isSuccess()) {
				logger.error("在支付宝找到了本次订单，说明已经转过了出现了幂等现象，更新数据库数据返回转账结束标志");
				logger.error(response.getBody());
				// 请求成功，把该次请求插入数据库
				insertMoneyRecord(workerWalletRecord, worker, 1);
				// 返回已经处理成功信息
				logger.error(JSONObject.parseObject(response.getBody())
						.get("alipay_fund_trans_toaccount_transfer_response"));
				buluResult.setStatus(0);
				buluResult.setMessage("提现成功");
				// {"msg":"Success","pay_date":"2017-04-15
				// 12:34:40","code":"10000","order_fee":"0.00","out_biz_no":"xia12345678890","order_id":"20170415110070001502310004692121","status":"SUCCESS"}
			} else {
				// 数据库中插入一条没有处理的提现记录(无论提现是否成功都要保存提现记录)
				insertMoneyRecord(workerWalletRecord, null, 0);
				// {"msg":"Business
				// Failed","code":"40004","sub_msg":"转账订单不存在","sub_code":"ORDER_NOT_EXIST"}
				logger.error("在支付宝没有查到本次转账订单，本次转账在支付宝方没有处理也没有出现幂等现象");
				JSONObject jsonObject = JSONObject.parseObject(response.getBody());
				logger.error(jsonObject.get("alipay_fund_trans_order_query_response"));
				buluResult.setStatus(1);
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return buluResult;
	}

	// 保存提现记录
	private void insertMoneyRecord(WorkerWalletRecord workerWalletRecord, Worker worker, Integer withdrawalStatus) {
		logger.error("=======================================保存工人提现记录=========================================");
		if (withdrawalStatus == 0) {// 申请提现
			workerWalletRecord.setStatus(withdrawalStatus);
			workerWalletRecord.setRecordEndTime(DateUtils.getDateTime());
			workerWalletRecord.setFinalLostMoney(workerWalletRecord.getWalletWithdrawals());
			workerWalletRecord.setIsDelete(0);
			workerWalletRecordService.insertWorkerWalletRecord(workerWalletRecord);
			/*
			 * moneyRecord.setWithdrawalStatus(withdrawalStatus);// 0代表提现没有被处理
			 * workerDao.insertMoneyRecord(moneyRecord);
			 */
		} else if (withdrawalStatus == 1) {// 提现完成
			workerWalletRecord.setStatus(withdrawalStatus);
			workerWalletRecord.setRecordEndTime(DateUtils.getDateTime());
			workerWalletRecord.setFinalLostMoney(workerWalletRecord.getWalletWithdrawals());
			workerWalletRecord.setIsDelete(0);
			workerWalletRecordService.insertWorkerWalletRecord(workerWalletRecord);
			/*
			 * moneyRecord.setWithdrawalStatus(withdrawalStatus);// 1代表提现已经处理
			 * workerDao.insertMoneyRecord(moneyRecord);
			 */
			// 更新工人提现后的金额
			Double newWlallet = Arith.sub(worker.getWallet(), workerWalletRecord.getWalletWithdrawals());
			Map<String, Object> map = new HashMap<>();
			map.put("workerId", workerWalletRecord.getWorkerId());
			map.put("wallet", newWlallet);
			workerDao.updateWalletComple(map);
		} else if (withdrawalStatus == 2) {// 付款方余额不足
			workerWalletRecord.setStatus(withdrawalStatus);
			workerWalletRecord.setFinalLostMoney(workerWalletRecord.getWalletWithdrawals());
			workerWalletRecord.setIsDelete(0);
			workerWalletRecordService.insertWorkerWalletRecord(workerWalletRecord);
			/*
			 * moneyRecord.setWithdrawalStatus(withdrawalStatus);// 0代表提现没有被处理
			 * workerDao.insertMoneyRecord(moneyRecord);
			 */
			// 更新工人提现后的金额
			Double newWlallet = Arith.sub(worker.getWallet(), workerWalletRecord.getWalletWithdrawals());
			Map<String, Object> map = new HashMap<>();
			map.put("workerId", workerWalletRecord.getWorkerId());
			map.put("wallet", newWlallet);
			workerDao.updateWalletComple(map);
		}
	}

	/**
	 * 如果工人提现但提现标志WithdrawalStatus=2 需要上传照片 调用该接口 本次存放的金融账号在表格中设置为1（默认）
	 * 目前只支持支付宝，没有传类似type的的字段没有办法判断添加提现到什么地方
	 */
	@Transactional
	public BuluResult auditPresentation(HttpServletRequest request, String alipyCode, String IdCard, String faceIDCard,
			String backIDCard) {
		BuluResult buluResult = new BuluResult();
		if (faceIDCard == null || backIDCard == null) {
			buluResult.setStatus(1);
			buluResult.setMessage("请先上传有效照片");
			return buluResult;
		}
		String sign = request.getParameter("sign");
		Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign, 7));
		Worker worker = workerDao.selectWorker(workerId);
		// 如果走这个接口，说明工人目前还没有提现能力
		List<BankCard> list = workerDao.selectNoUseBank(workerId);
		if (null != list && list.size() > 0) {
			updateAuditPresentation(list, workerId, worker);
		}
		BankCard bankCard = new BankCard();
		bankCard.setBoundBank("支付宝");
		bankCard.setAlipayNum(alipyCode);
		bankCard.setPeopleId(workerId);// 工人账户
		bankCard.setStatus(1);// 默认
		bankCard.setType(0);// 0代表工人
		bankCardDao.insertBankCard(bankCard);
		if (null == worker.getFaceIdCard() || worker.getFaceIdCard().equals("")) {
			worker.setFaceIdCard(faceIDCard);
			worker.setBackIdCard(backIDCard);
			worker.setIdCard(IdCard);
			worker.setWithdrawalStatus(0);
			worker.setAlipayNum(alipyCode);
			workerDao.updateWorker(worker);
		}
		buluResult.setData(worker);
		buluResult.setStatus(0);
		buluResult.setMessage("提现正在审核中....");
		return buluResult;
	}

	/**
	 * 清理之前的无效提现数据
	 * 
	 * @param list
	 * @param workerId
	 */
	private void updateAuditPresentation(List<BankCard> list, Integer workerId, Worker worker) {
		JSONArray idArray = new JSONArray();
		for (int a = 0; a < list.size(); a++) {
			idArray.add(list.get(a).getBankCardId());
		}
		// 将其对应的id给为删除状态
		workerDao.updateNoUseBank(idArray);
		// 将工人之前对应保存的也都清空,并且设置为空
		if (null != worker.getFaceIdCard() && !("".equals(worker.getFaceIdCard()))) {
			worker.setFaceIdCard(null);
			worker.setBackIdCard(null);
			worker.setIdCard(null);
			worker.setWithdrawalStatus(2);
			worker.setAlipayNum(null);
			workerDao.updateWorker(worker);
		}
	}

	// @Override
	// public BuluResult selectCollegeMsg(HttpServletRequest request) {
	// // TODO Auto-generated method stub
	// String sign = request.getParameter("sign");
	// Integer workerId = Integer.parseInt(JedisUtil.getSessionIDForApp(sign,
	// 7));
	// CollegeMsg collegeMsg = workerDao.selectCollegeMsg(workerId);
	// BuluResult buluResult = new BuluResult();
	// buluResult.setData(collegeMsg);
	// buluResult.setStatus(0);
	// return buluResult;
	// }

}
